home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15610 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  66 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: What are the differences between structures and classes in C++ ?
  5. Message-ID: <marnoldDpHvED.E0t@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <4k5m65$av@hpscit.sc.hp.com> <DpG53J.Hsz@presby.edu> <4k83j3$a56@hpscit.sc.hp.com>
  8. Date: Sun, 7 Apr 1996 13:42:13 GMT
  9. Sender: marnold@netcom23.netcom.com
  10.  
  11. Raghuveera Ravinutala <raghur> writes:
  12.  
  13. ><jtbell@presby.edu (Jon Bell)> wrote :
  14. >>the only *formal* difference between a class and a
  15. >>struct is that by default, class members are private whereas struct
  16. >>members are public.
  17.  
  18. >Thanx. I am aware of this difference. I would like to know more, like,
  19. >inheritence etc..
  20.  
  21. There is none, other the default protection state of members.  This is
  22. what the original poster meant by only a formal difference.  All the
  23. rules you know about classes in C++ also apply to structs.  For example,
  24. the following two versions of Foo are precisely equivalent...
  25.  
  26. // version #1, using class keyword
  27.  
  28. class Foo: public Bar
  29.    {
  30.    public:
  31.       // some public members
  32.    private:
  33.       // some private members
  34.    };
  35.  
  36. // version #2, using struct keyword
  37.  
  38. struct Foo: public Bar
  39.    {
  40.    public:
  41.       // some public members
  42.    private:
  43.       // some private members
  44.    };
  45.  
  46. ...since the protection of all members is explicit.  And, the fact that 
  47. Foo is a struct or class doesn't affect the inheritance from Bar (that
  48. is, whatever public interface Bar defines becomes part of Foo's, in 
  49. either case, as expected).
  50.  
  51. Structs can inherit from other structs and classes and can be inherited
  52. by other structs and classes.  Structs can have virtual functions, member 
  53. operators, constructors and destructors, you can create template structs,
  54. and so on.
  55.  
  56. In other words, structs *are* classes.
  57.  
  58. Regards,
  59. -------------------------------------------------------------------------
  60. Matt Arnold                       |        | ||| | |||| |  | | || ||
  61. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  62. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  63. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  64. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  65. -------------------------------------------------------------------------
  66.